package com.rwhansen.humidor;

import java.math.BigDecimal;
import java.util.Calendar;

public class Cigar {

	private String name;
	private String brand;
	private String size;
	private String body;
	private String description;
	private int gauge;
	private double length;
	private String country;
	private int caRating;
	private int caRatingYear;
	private int dayAddHumidor;
	private int monthAddHumidor;
	private int yearAddHumidor;
	private BigDecimal price;
	private String purchaseLocation;
	private int daySmoked;
	private int monthSmoked;
	private int yearSmoked;
	private int myRating;
	private String comment;
	private String humidor;
	private String dateAddHumidor;
	private String dateSmoked;
	private int quantity;
	private String modified;
	private int minutesSmoked;
	private String fullySmoked;
	private boolean wishList;
	private boolean recomend;
	private String wrapper;
	private String Binder;
	private String Filler;
	private String wrapperOrigin;
	private String BinderOrigin;
	private String FillerOrigin;
	
	
	
	public String getWrapperOrigin() {
		return wrapperOrigin;
	}

	public void setWrapperOrigin(String wrapperOrigin) {
		this.wrapperOrigin = wrapperOrigin;
	}

	public String getBinderOrigin() {
		return BinderOrigin;
	}

	public void setBinderOrigin(String binderOrigin) {
		BinderOrigin = binderOrigin;
	}

	public String getFillerOrigin() {
		return FillerOrigin;
	}

	public void setFillerOrigin(String fillerOrigin) {
		FillerOrigin = fillerOrigin;
	}

	public String getWrapper() {
		return wrapper;
	}

	public void setWrapper(String wrapper) {
		this.wrapper = wrapper;
	}

	public String getBinder() {
		return Binder;
	}

	public void setBinder(String binder) {
		Binder = binder;
	}

	public String getFiller() {
		return Filler;
	}

	public void setFiller(String filler) {
		Filler = filler;
	}

	public String getModified() {
		return modified;
	}

	public void setModified(String modified) {
		this.modified = getDate(modified);
	}

	public int getMinutesSmoked() {
		return minutesSmoked;
	}

	public void setMinutesSmoked(int minutesSmoked) {
		this.minutesSmoked = minutesSmoked;
	}

	public String getFullySmoked() {
		return fullySmoked;
	}

	public void setFullySmoked(String fullySmoked) {
		this.fullySmoked = fullySmoked;
	}

	public boolean isWishList() {
		return wishList;
	}

	public void setWishList(boolean wishList) {
		this.wishList = wishList;
	}

	public boolean isRecomend() {
		return recomend;
	}

	public void setRecomend(boolean recomend) {
		this.recomend = recomend;
	}

	public Cigar(String cigarName) {
		name = cigarName;
	}
	
	public String getName() {
		return name;
	}
	
	public Boolean setName(String name) {
		this.name = name;
		return true;
	}
	
	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

//	public String getDate(String date) {
//		//TODO: Date Validation
//				//get fist char
//				//look for patterns
//		
//		Calendar cal = Calendar.getInstance();
//		int day = cal.get(Calendar.DATE);
//		int month = cal.get(Calendar.MONTH) + 1;
//		int year = cal.get(Calendar.YEAR);
//		date = date.toUpperCase();
//		
//		int dateLength = date.length();
//		char firstChar;
//		char secondChar = '!';
//		
//		
//
//		if (dateLength == 2) {
//			secondChar = date.charAt(1);
//		}
//		if (dateLength >= 1) {
//			firstChar = date.charAt(0);
//			if (firstChar == 'T') {
//				if (secondChar == '-') {
//					day = day - 1;
//					if (day < 1) {
//						day = getDaysInMonth(month, year);
//					}
//					return month + "/" + day + "/" + year;
//				}
//				return month + "/" + day + "/" + year;
//			}
//			
//			if (firstChar == 'W') {
//				day = day - 7;
//				if (day < 1) { 
//					month = month -1;
//					day = getDaysInMonth(month, year) - day;
//					if (month < 1) {
//						month = 12;
//						year = year - 1;
//					}
//				}
//				return month + "/" + day + "/" + year;
//			}
//			
//			if (firstChar == 'Y') {
//				year = year - 1;
//				return month + "/" + day + "/" + year;
//			}
//		}
//		
//		if(date.equals(date)) {
//			return date + "!";
//		}
//		return "Invalid Date";
//	}
	
	public String getDate(String date) {
		//TODO: Date Validation
				//get fist char
				//look for patterns
		
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		date = date.trim();
		date = date.toUpperCase();
		 
		java.text.DecimalFormat twoDigit = new  
		java.text.DecimalFormat("#00.###");  
		twoDigit.setDecimalSeparatorAlwaysShown(false);  	
		
		if (date.equals("T")) {
			return year + "/" + twoDigit.format(month) + "/" + twoDigit.format(day);
		} else if (date.equals("T-")) {
			day = day - 1;
			return year + "/" + twoDigit.format(month) + "/" + twoDigit.format(day);
		} else if (date.equals("W-")) {
			day = day - 7;
			if (day < 1) { 
				month = month -1;
				day = getDaysInMonth(month, year) - day;
				if (month < 1) {
					month = 12;
					year = year - 1;
				}
			}
			return year + "/" + twoDigit.format(month) + "/" + twoDigit.format(day);
		} else if (date.equals("Y-")) {
			year = year - 1;
			return year + "/" + twoDigit.format(month) + "/" + twoDigit.format(day);
		} else if(date.equals(date)) {
			return date;
		}
		return "Invalid Date";
	}
	
	/**
	 * Uses the month and year to return the maximum number of days in that month
	 * @param month
	 * @param year
	 * @return The maximum days in the month
	 */
	public int getDaysInMonth(int month, int year) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
			return 31;
		} else if (month == 2) {
			if (year == 2004 || year == 2008 || year == 2012 || year == 2016 || year == 2020 || year == 2024) {
				return 29;
			}
			return 28;
		} else {
			return 31;
		}
	}
	
	public void setDateAddHumidor(String dateAddHumidor) {
		//TODO: Change to Boolian
		this.dateAddHumidor = getDate(dateAddHumidor);
	}

	public String getDateAddHumidor() {
		return dateAddHumidor;
	}
	
	public String getDateSmoked() {
		return dateSmoked;
	}

	public void setDateSmoked(String dateSmoked) {
		this.dateSmoked = getDate(dateSmoked);
	}

	public String getBrand() {
		return brand;
	}

	public Boolean setBrand(String brand) {
		if (brand.length() < 50) {
			this.brand = brand;
			return true;
		}
		return false;
	}

	public String getSize() {
		return size;
	}

	public Boolean setSize(String size) {
		if (size.length() < 50) {
			this.size = size;
			return true;
		}
		return false;
	}

	public String getBody() {
		return body;
	}

	public Boolean setBody(String body) {
		if (body.length() < 50) {
			this.body = body;
			return true;
		}
		return false;
	}

	public String getDescription() {
		return description;
	}

	//TODO: Fix this
	public Boolean setDescription(String description) {
		if (description.length() < 200) {
			this.description = description;
			return true;
		}
		this.description = description;
		return true;
	}

	public int getGauge() {
		return gauge;
	}

	public Boolean setGauge(int gauge) {
		if (gauge > 0) {
			this.gauge = gauge;
			return true;
		}
		return false;
	}

	public double getLength() {
		return length;
	}

	public Boolean setLength(double length) {
		if (length > 0) {
			this.length = length;
			return true;
		}
		return false;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		//TODO add validation check
		this.country = country;
	}

	public int getCaRating() {
		return caRating;
	}

	public Boolean setCaRating(int caRating) {
		if ((caRating >= 0) && (caRating <= 100)) {
			this.caRating = caRating;
			return true;
		}
		return false;
	}

	public int getCaRatingYear() {
		return caRatingYear;
	}

	public Boolean setCaRatingYear(int caRatingYear) {
		//TODO need better validation check here
		if ((caRatingYear >= 1992) && (caRatingYear <= 2050)) {
			this.caRatingYear = caRatingYear;
			return true;
		}
		return false;
	}

	public int getDayAddHumidor() {
		return dayAddHumidor;
	}

	public Boolean setDayAddHumidor(int dayAddHumidor) {
		//TODO need better validation check
		// like checking number of days in a month (feb only has 28)
		if ((dayAddHumidor > 0) && (dayAddHumidor <= 31)) {
			this.dayAddHumidor = dayAddHumidor;
			return true;
		}
		return false;
	}

	public int getMonthAddHumidor() {
		return monthAddHumidor;
	}

	public Boolean setMonthAddHumidor(int mounthAddHumidor) {
		if ((mounthAddHumidor > 0) && (mounthAddHumidor <= 12)) {
			this.monthAddHumidor = mounthAddHumidor;
			return true;
		}
		return false;
	}

	public int getYearAddHumidor() {
		return yearAddHumidor;
	}

	public Boolean setYearAddHumidor(int yearAddHumidor) {
		if ((yearAddHumidor > 2005) && (yearAddHumidor <= 2050)) {
			this.yearAddHumidor = yearAddHumidor;
			return true;
		}
		return false;
	}
	
	public String getHumidor() {
		return humidor;
	}

	public Boolean setHumidor(String humidor) {
		//TODO add validation check - how do we validate the humidor, or is this the name only?
		//Answer: This will be automatically set when the cigar is added to the humidor, the only two values will be "My Humidor" or "Not Kept"
		if ((humidor == "My Humidor") || (humidor == "Not Kept")){
			this.humidor = humidor;
			return true;
		}
		return false;
	}

	public BigDecimal getPrice() {
		return price;
	}
	
	private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;
	private static int DECIMALS = 2;
	
	public boolean setPrice(BigDecimal price) {
		if (true){
			this.price = price;
			price.setScale(DECIMALS, ROUNDING_MODE);
		}
		return false;
	}

	public String getPurchaseLocation() {
		return purchaseLocation;
	}

	public Boolean setPurchaseLocation(String purchaseLocation) {
		if (purchaseLocation.length() < 200) {
			this.purchaseLocation = purchaseLocation;
			return true;
		}
		return false;
	}

	public int getDaySmoked() {
		return daySmoked;
	}

	public Boolean setDaySmoked(int daySmoked) {
		//TODO need better validation check, like number of days in a month
		if ((daySmoked > 0) && (daySmoked <= 31)) {
			this.daySmoked = daySmoked;
			return true;
		}
		return false;
	}

	public int getMonthSmoked() {
		return monthSmoked;
	}

	public Boolean setMonthSmoked(int monthSmoked) {
		if ((monthSmoked > 0) && (monthSmoked <= 12)) {
			this.monthAddHumidor = monthSmoked;
			return true;
		}
		return false;
	}

	public int getYearSmoked() {
		return yearSmoked;
	}

	public Boolean setYearSmoked(int yearSmoked) {
		//TODO need better validation check
		if ((yearSmoked > 2005) && (yearSmoked <= 2050)) {
			this.yearSmoked = yearSmoked;
			return true;
		}
		return false;
	}

	public int getMyRating() {
		return myRating;
	}

	public Boolean setMyRating(int myRating) {
		if ((myRating >= 0) && (myRating <= 10)) {
			this.myRating = myRating;
			return true;
		}
		return false;
	}

	public String getComment() {
		return comment;
	}

	public Boolean setComment(String comment) {
		if (comment.length() < 2000) {
			this.comment = comment;
			return true;
		}
		return false;
	}

	
	
	public void setTest() {
		setName("Test Name");
		setBrand("Test Brand");
		setSize("Test Size");
		setBody("Test Body");
		setGauge(69);
		setLength(6.9);
		setCountry("Test Country");
		setCaRating(69);
		setCaRatingYear(1969);
		setDescription("Test Description");
		setMonthAddHumidor(12);
		setDayAddHumidor(34);
		setYearAddHumidor(5678);
		setDateAddHumidor("T");
		setQuantity(0);
		setMonthSmoked(98);
		setDaySmoked(76);
		setYearSmoked(5432);
		setMyRating(9);
		setComment("Test Comments");
		setPrice(new BigDecimal(12345.6789));
		setPurchaseLocation("Test Location");
	}
	
	public void setClearCigar() {
		setName("");
		setBrand("");
		setSize("");
		setBody("");
		setGauge(0);
		setLength(0.0);
		setCountry("");
		setCaRating(0);
		setCaRatingYear(0000);
		setDescription("");
		setMonthAddHumidor(0);
		setDayAddHumidor(0);
		setYearAddHumidor(0);
		setDateAddHumidor("T");
		setQuantity(0);
		setMonthSmoked(0);
		setDaySmoked(0);
		setYearSmoked(0);
		setMyRating(0);
		setComment("");
		setPrice(new BigDecimal(0.00));
		setPurchaseLocation("");
	}

	public void setNew() {
		setBrand("Unknown Brand");
		setSize("Unknown Size");
		setBody("Unknown Body");
		setGauge(0);
		setLength(0.0);
		setCountry("Unknown Country");
		setCaRating(0);
		setCaRatingYear(0000);
		setDescription("No Description Availible");
		setMonthAddHumidor(0);
		setDayAddHumidor(0);
		setYearAddHumidor(0);
		setDateAddHumidor("");
		setQuantity(0);
		setMonthSmoked(0);
		setDaySmoked(0);
		setYearSmoked(0);
		setDateAddHumidor("");
		setMyRating(0);
		setComment("");
		setPrice(new BigDecimal(0.00));
		setPurchaseLocation("");
	}

	
}
